package com.example.huanhang.changzhouapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;

import com.example.huanhang.changzhouapp.R;
import com.example.huanhang.changzhouapp.adapter.FeileiAdapter;
import com.example.huanhang.changzhouapp.bean.Feilei;
import com.example.huanhang.changzhouapp.util.ThreadUtil;
import com.gyf.barlibrary.ImmersionBar;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @author lei
 * @desc 获取分类列表
 * @date 2018/8/26 0026 -- 下午 2:36.
 * 个人博客站: http://www.bestlei.top
 */
public class FeiLeiActivity extends AppCompatActivity {

    private RecyclerView mInRecycleview;
    FeileiAdapter flAdapter;
    List<Feilei> mList = new ArrayList<>();
    private ProgressBar mMainProgress;
    private AppCompatImageView mIvBack;
    private AppCompatTextView mTvTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fei_lei);
        initView();
        initData();
    }

    private void initView() {
        mInRecycleview = (RecyclerView) findViewById(R.id.in_recycleview);
        mMainProgress = (ProgressBar) findViewById(R.id.main_progress);
        mMainProgress.setVisibility(View.VISIBLE);
        mInRecycleview.setLayoutManager(new LinearLayoutManager(this));
        flAdapter = new FeileiAdapter(R.layout.item_feilei, mList);
        flAdapter.setOnItemClickListener((adapter, view, position) -> {
            Feilei feilei = mList.get(position);
            Intent intent = new Intent(FeiLeiActivity.this, ListActivity.class);
            intent.putExtra("fl_name", feilei.getFlName());
            startActivity(intent);
        });
        mInRecycleview.setAdapter(flAdapter);
        mIvBack = (AppCompatImageView) findViewById(R.id.iv_back);
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mTvTitle = (AppCompatTextView) findViewById(R.id.tv_title);
        ImmersionBar.with(this).statusBarColor(R.color.title).init();
    }

    private void initData() {
        mTvTitle.setText("标准文件分类");
        getAdapterData();
    }

    private void getAdapterData() {
        if (mList.size() != 0) {
            mList.clear();
        }
        requestNet();

    }

    private void requestNet() {
        ThreadUtil.runOnBackThread(() -> {
            //已经排除非pdf文件
            final String sql = "SELECT DISTINCT lccb_fl FROM basic  WHERE  SUBSTRING(lccb_path, -3)='pdf' and lccb_fl not in ('程序文件','不确定度','质量表格','质量手册附件','质量手册') order by LENGTH(lccb_fl) ";
            try {
                ResultSet resultSet = ThreadUtil.result(sql);
                while (resultSet.next()) {
                    Feilei fl = new Feilei();
                    fl.setFlName(resultSet.getString("lccb_fl"));
                    mList.add(fl);
                }
                querySuccess();
                resultSet.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });

    }

    private void querySuccess() {
        ThreadUtil.runOnUiThread(() -> {
            flAdapter.notifyDataSetChanged();
            mMainProgress.setVisibility(View.GONE);
        });
    }
}
